<template>
  <van-cell-group class="commentItem">
    <van-cell>
      <van-image
        class="commentItem-avatar"
        slot="icon"
        round
        fit="cover"
        :src="comment.avatar"
      />
      <div slot="title">
        <div class="commentItem-name">{{ comment.nickname }}</div>
        <div class="commentItem-text">
          {{ comment.commentText }}
        </div>
        <div>
          <span class="commentItem-time">
            {{ comment.time | datetime('MM:DD HH:mm') }}</span
          >
          <van-button
            v-if="comment.type === 'a'"
            class="commentItem-btn"
            size="small"
            round
            type="default"
            @click="$emit('reply-click', comment)"
            >{{ comment.replycount }} 回复</van-button
          >
        </div>
      </div>
      <div
        slot="right-icon"
        class="commentItem-click"
        @click="setCommentStatus()"
      >
        <van-icon
          :color="comment.is_liking ? 'red' : '#000'"
          :name="comment.is_liking ? 'good-job' : 'good-job-o'"
        />
        <span class="lick-count">{{ comment.praisecount }}</span>
      </div>
    </van-cell>
  </van-cell-group>
</template>
<script>
import { addCommentsPraise, delCommentsPraise } from '@/api/comment'
import { Toast } from 'vant'
export default {
  name: 'commentItem',
  data () {
    return {}
  },
  created () {},
  methods: {
    async setCommentStatus () {
      Toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      try {
        if (!this.comment.is_liking) {
          // 收藏
          await addCommentsPraise({
            type: 'c', // c-评论点赞,r-回复点赞
            commentId: this.comment.id
          })
          this.comment.praisecount++
        } else {
          // 取消收藏
          await delCommentsPraise({
            type: 'c', // c-评论点赞,r-回复点赞
            commentId: this.comment.id
          })
          this.comment.praisecount--
        }
        this.comment.is_liking = !this.comment.is_liking
        Toast.success(`${this.comment.is_liking ? '点赞成功' : '已取消点赞'}`)
      } catch (error) {
        return error
      }
    }
  },
  components: {},
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  watch: {},
  computed: {},
  mounted () {}
}
</script>

<style lang="less" scoped>
.commentItem {
  .commentItem-avatar {
    height: 36px;
    width: 36px;
    margin-right: 5px;
  }
  /deep/ .commentItem-name {
    font-size: 14px;
    color: #1989fa;
  }
  /deep/ .commentItem-text {
    font-size: 16px;
  }
  .commentItem-time {
    font-size: 10px;
  }
  .commentItem-btn {
    height: 20px;
  }
}
</style>
